package cn.kgc.vue.service.impl;

import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.response.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.vue.entity.Course;
import cn.kgc.vue.service.CourseService;
import cn.kgc.vue.mapper.CourseMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author mengshun
* @description 针对表【sys_course】的数据库操作Service实现
* @createDate 2024-10-06 08:39:05
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>implements CourseService{


    @Override
    public ResponseResult courseList() {
      List<Course> courses =  baseMapper.selectCourseList();
        return ResponseResult.success().data("courseList",courses);
    }

    @Override
    public ResponseResult coursePage(QueryCondition<Course> queryCondition) {

        Page<Course> coursePage = new Page<>(queryCondition.getPage(), queryCondition.getLimit());

        String courseName = queryCondition.getSearchParams().getCourseName();
        LambdaQueryWrapper<Course> lambda = new QueryWrapper<Course>().lambda();
        lambda.like(StringUtils.isNotBlank(courseName),Course::getCourseName,courseName)
                .orderByDesc(Course::getCreateTime);

        baseMapper.selectPage(coursePage, lambda);
        List<Course> records = coursePage.getRecords();
        long total = coursePage.getTotal();

        return ResponseResult.success().data("total",total)
                .data("tableData",records);
    }
}




